package FeedMaker;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XmlParser
{
	public Document doc_;
	
	public XmlParser( String filename )
	{
		if( filename == null || filename.length() == 0 )
			return;
		
		File path = new File( filename.substring( 0, filename.lastIndexOf("/") ) );
		
		if( !path.isDirectory() )
		{
			path.mkdirs();
		}
		
		File file = new File( filename );
		
		if( file.isFile() )
		{
			try {
				parse( new FileInputStream(file) );
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public XmlParser( InputStream is )
	{
		parse( is );
	}
	
	private void parse( InputStream is )
	{
		if( is != null )
		{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			
			DocumentBuilder db;
			try
			{
				db = dbf.newDocumentBuilder();

				try {
					doc_ = db.parse(is);
					
					doc_.getDocumentElement().normalize();
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
			}
			catch( Exception e )
			{
				e.printStackTrace();
			}
		}
	}
}
